package oack1;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;


public class InitDatabase {
	public static void initDB () throws SQLException
    {
        Connection conn = null;

        try
        {
            String userName = "root";
            String password = "pass123";
            String url = "jdbc:mysql://localhost/nostalgia";
            Class.forName ("com.mysql.jdbc.Driver").newInstance ();
            conn = DriverManager.getConnection (url, userName, password);
            System.out.println ("Database connection established");
        }
        catch (Exception e)
        {
            System.err.println ("Cannot connect to database server");
        }
        
        Statement s = conn.createStatement ();
        int count;
        s.executeUpdate ("DROP TABLE IF EXISTS UserDetail");
        s.executeUpdate (
                    "CREATE TABLE UserDetail ("
                    + "id INT UNSIGNED NOT NULL AUTO_INCREMENT,"
                    + "PRIMARY KEY (id),"
                    + "FirstName VARCHAR(45), LastName VARCHAR(40), Gender VARCHAR(1), LinkedIn VARCHAR(50), Profession VARCHAR(50), IIITMail VARCHAR(50))");
        count = s.executeUpdate (
                    "INSERT INTO UserDetail (FirstName, LastName, Gender, LinkedIn, Profession, IIITMail)"
                    + " VALUES"
                    + "('Akshit', 'Nanda', 'M', 'http://linkedin.com/akshit', 'Student', 'akshit09009@iiitd.ac.in')");
        s.close ();
        //System.out.println (count + " rows were inserted");
    }
	
}
